<?php
/**
 * ArchivePress "Logs" Dashboard widget
 *
 * @author University of London Computer Centre
 * @version 1.1.0
 * @package ArchivePress
 * @subpackage widgets
 * @global ArchivePress $ArchivePress
 */

global $ArchivePress;

// There are two possible views for this plugin
// "Show logs" View and "Edit configuration" View
$edit = "";
if(isset($_GET["edit"])){
    $edit = attribute_escape($_GET["edit"]);
}

// Edit Configuration View
if($edit == "archivepress_dashboard_log_widget"){
    // for js disabled
    echo "<noscript><div class='postbox-title-action' style='text-align:right' ><a href='".get_bloginfo( 'wpurl' )."/wp-admin/index.php'>".__('Cancel',ARCHIVEPRESS)."</a></div></noscript>";

    // config form
    echo'
    <form action="" method="post" class="dashboard-widget-control-form">
    '.wp_nonce_field(ARCHIVEPRESS . "-update-options_log-options").'
        <p>
            <label for="archivepress_dashboard_log_widget_type">'.__("Which logs do you want to display?",ARCHIVEPRESS).'</label>
            <select id="archivepress_dashboard_log_widget_type" name="archivepress_dashboard_log_widget_type">';
        if(get_option(WP_ARCHIVEPRESS_PREFIX."log_widget_show") == ArchivePressLog::LOG_BASIC){
            echo'<option value="'.ArchivePressLog::LOG_BASIC.'" selected="selected">'.__("Basic",ARCHIVEPRESS).'</option>
                <option value="'.ArchivePressLog::LOG_DEBUG.'">'.__("Debug",ARCHIVEPRESS).'</option>';
        }else {
            echo'<option value="'.ArchivePressLog::LOG_BASIC.'">'.__("Basic",ARCHIVEPRESS).'</option>
                <option value="'.ArchivePressLog::LOG_DEBUG.'" selected="selected">'.__("Debug",ARCHIVEPRESS).'</option>';
        }

        echo'
            </select>
        </p>
        <p>
            <label for="archivepress_dashboard_log_widget_items">How many items would you like to display?</label>
            <select id="archivepress_dashboard_log_widget_items" name="archivepress_dashboard_log_widget_items">';
        for($i=1;$i<=10;$i++){
            $selected = "";
            if(get_option(WP_ARCHIVEPRESS_PREFIX."log_widget_items") == ($i*5)){
                $selected = "selected='selected'";
            }
            echo '<option value="'.($i*5).'" '.$selected.'>'.($i*5).'</option>';
        }
        echo'
            </select>
        </p>
        <p class="submit">
            <input id="archivepress_dashboard_log_widget_save" value="'.__("Save",ARCHIVEPRESS).'" type="submit">
        </p>
    </form>';

// Show Logs View
} else {
    // print logs
    $logs = array();
    $logs = $ArchivePress->getLogs();

    // for js disabled
    echo "<noscript><div class='postbox-title-action' style='text-align:right' ><a href='?edit=archivepress_dashboard_log_widget#archivepress_dashboard_log_widget'>".__('Configure',ARCHIVEPRESS)."</a></div></noscript>";


    echo'<p class="sub">'.__("Logs",ARCHIVEPRESS).'</p>
    <div class="table">
        <table>
        <tbody>';
    if(count($logs)==0){
        echo '<tr class="first">
                <td class="first b">'.__("No log to display",ARCHIVEPRESS).'</td>
                <td class="t"></td>
            </tr>';
    } else {
        $first = true;
        foreach($logs as $log){
            // clean it
            $log["message"] = $ArchivePress->cleanOutput($log["message"]);
            // if the string is too long I have to split it into shorter substrings
            if(strlen($log["message"]) > 40){
                $tmpMessage = "";
                for($i = 0; $i < ceil(strlen($log["message"]) / 50); $i++){
                    $tmpMessage .= substr($log["message"], $i * 50, 49)."-<br />";
                }
                $log["message"] = $tmpMessage;
            }
            if($first){
                echo '<tr class="first">
                    <td class="first b">'.mysql2date('d-m-Y H:i',$log["date"]).'</td>
                    <td class="t">'.$log["message"].'</td>
                </tr>';
                $first = false;
            } else {
                echo '<tr>
                    <td class="first b">'.mysql2date('d-m-Y H:i',$log["date"]).'</td>
                    <td class="t">'.$log["message"].'</td>
                </tr>';
            }
        }
    }
    echo '</tbody>
        </table>
    </div>';
}

?>